import {
  qnToken
} from '@/api/common'

export const getQueryString = function (str) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == str) {
      return pair[1];
    }
  }
  return (false);
}

function loadQiniuJs() {
  return new Promise(function (resolve, reject) {
    if (typeof qiniu !== 'undefined') {
      resolve(qiniu)
      return true
    }
    let script = document.createElement('script')
    script.type = 'text/javascript'
    script.src =
      'qiniu/qiniu.min.js'
    script.onerror = reject
    script.onload = () => resolve(qiniu)
    document.head.appendChild(script)
  })
}

// 生成七牛文件key
export const getQiniuKey = (file, fileName) => {
  var fielLocal = file.name.split('.')
  var fielType = '.' + fielLocal[fielLocal.length - 1]
  var currentTime = new Date().getTime() + Math.ceil(Math.random() * 99) //根据时间戳生成唯一标识
  var fielName_Local = currentTime
  var key = ''
  var dirUrl = 'app/'
  if (fileName) {
    key = dirUrl + fileName + fielType
  } else {
    key = dirUrl + fielName_Local + fielType
  }
  return key
}

// 上传文件
export const qiniuUpload = (file, observer = {}, fileName) => {
  qnToken().then((res) => {
    loadQiniuJs().then(() => {
      let key = getQiniuKey(file, fileName)
      let token = res.data
      let putExtra = {
        mimeType: ['*'],
        fileName:key.replace('app/','')
      }
      let config = {
        upprotocol: 'http:',
        disableStatisticsReport: true,
        forceDirect:true
      }
      var observable = qiniu.upload(file, key, token, putExtra, config)
      var subscription = observable.subscribe(observer)
    })
  })
}

export var getTimeDuration = (seconds) => {
  let day = Math.floor(seconds / 86400)
  let hour = Math.floor((seconds - 86400 * day) / 3600)
  let minute = Math.floor((seconds - 86400 * day - hour * 3600) / 60)
  let sec = parseInt(seconds - 86400 * day - hour * 3600 - minute * 60)
  let valueArr = [{
    value: day,
    unit: '天'
  }, {
    value: hour,
    unit: '小时'
  }, {
    value: minute,
    unit: '分钟'
  }, {
    value: sec,
    unit: '秒'
  }]
  return valueArr.filter(n => {
    return n.value > 0
  }).splice(0, 2)
}

export const getDataByKey = function (key) {
  var u = navigator.userAgent;
  var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  var isIOS = u.indexOf('iPhone') > -1 || u.indexOf('iPad') > -1; //IOS终端
  if (isAndroid) {
    var data = nativeService.getDataByKey(key);
    return data;
  } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.jssaveDataforIOS) {
    window.webkit.messageHandlers.jssaveDataforIOS.postMessage(key);
  } else {
    return localStorage.getItem(key);
  }
}

export const saveDataByKey = function (key, value) {
  try {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isIOS = u.indexOf('iPhone') > -1 || u.indexOf('iPad') > -1; //IOS终端
    if (isAndroid) {
      try {
        nativeService.saveDataByKey(key, value);
      } catch (e) {

      }
    } else if (isIOS) {
      try {
        var bykey = {
          'key': key,
          'value': value
        };
        window.webkit.messageHandlers.saveDataforIOS.postMessage(bykey);
      } catch (e) {

      }
    } else {
      localStorage.setItem(key, value);
    }
  } catch (e) {

  }
}
